module.exports = function ($scope, $stateParams, $http, $q, FileUploader, str2date, date2str, $resource, toaster, productid, $modalInstance, auditing) {

	var id = $stateParams.id || productid;

	//产品对象，保存着产品的基本信息
	$scope.saleobj = {
		'id': id || '',
	};

	$scope.util = {
		'str2date': str2date,
		'date2str': date2str,
		'auditing': auditing,
		'$modalInstance': $modalInstance,
		'apply_note_temp': '',
		'showFlag': false
	};
	//销售品功能列表
	$scope.funobj = {};

	//基本信息中需要提前弄好的信息。
	$scope.baseinfo = {
		'imageshow': {},
		'dateshow': {},
	};

	var uploader1 = $scope.uploader1 = new FileUploader({
		url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=ticket&selfdir=product'+Date.parse(new Date())
	});
	uploader1.filters.push({
		name: 'imageFilter',
		fn: function (item /*{File|FileLikeObject}*/, options) {
			var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
			return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
		}
	});
	uploader1.onSuccessItem = function (fileItem, response, status, headers) {
		$scope.saleobj.top_pic = response.savename;
	};

	var uploader2 = $scope.uploader2 = new FileUploader({
		url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=ticket&selfdir=product'+Date.parse(new Date())
	});
	uploader2.filters.push({
		name: 'imageFilter',
		fn: function (item /*{File|FileLikeObject}*/, options) {
			var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
			return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
		}
	});
	uploader2.onSuccessItem = function (fileItem, response, status, headers) {
		$scope.saleobj.logo = response.savename;
	};

	$scope.baseinfo.imageshow.uploader1 = uploader1;
	$scope.baseinfo.imageshow.uploader2 = uploader2;


	$scope.baseinfo.dateshow.periodstart = {
		'date': new Date(),
	};

	$scope.baseinfo.dateshow.periodend = {
		'date': new Date($scope.baseinfo.dateshow.periodstart.date.getFullYear(), 11, 31),
	};

	$scope.baseinfo.dateshow.open = function (obj) {
		obj.opened = true;
	};


	// $scope.apply_note_temp = $scope.saleobj.apply_note ? $scope.saleobj.apply_note : '审核意见必填';

	$scope.pass = function () {
		if (!$scope.saleobj.apply_note) {
			toaster.error({ title: "提示", body: '审核意见必填' })
			return false;
		}
		$resource('/api/ac/tc/ticketSaleService/updateSaleApplyPass', {}, {})
			.save({
				'id': $scope.saleobj.id,
				'apply_note': $scope.saleobj.apply_note
			}, function (res) {
				if (res.errcode !== 0) {
					toaster.error({ title: "提示", body: res.errmsg });
					toaster.error({ title: "提示", body: res.errmsg });
					return;
				}
				toaster.success({ title: "提示", body: "操作成功!" });
				// toaster.success({title:"",body:'亲,记得重新保存商客专属设置信息哦💐💐'});
				$scope.util.$modalInstance.close();
			});
	};

	$scope.nopass = function () {
		if (!$scope.saleobj.apply_note) {
			toaster.error({ title: "提示", body: '审核意见必填' })
			return false;
		}
		$resource('/api/ac/tc/ticketSaleService/updateSaleApplyNoPass', {}, {})
			.save({
				'id': $scope.saleobj.id,
				'apply_note': $scope.saleobj.apply_note
			}, function (res) {
				if (res.errcode !== 0) {
					toaster.error({ title: "提示", body: res.errmsg });
					// toaster.error({ title: "提示", body: res.errmsg });
					return;
				}
				// toaster.success({ title: "提示", body: "操作成功!" });
				toaster.success({ title: "提示", body: "操作成功!" });
				$scope.util.$modalInstance.close();
			});
	};


	$scope.auditingFlag = false;
	$scope.auditingInfo = 5;

	function timing() {
		if ($scope.auditingInfo > 0) {
			$scope.auditingInfo--;
			$scope.$apply();
		} else if ($scope.auditingInfo == 0) {
			clearInterval(intervalProcess);
			$scope.auditingFlag = true;
			$scope.$apply();
		}
	}
	var intervalProcess = setInterval(function () { timing() }, 1000);

	$scope.showattrarr = [
		{
			key: 'market_price_display',
			position: 'left',
			// before: '<font color=red >门市价格: ¥</font>'
		},
		{
			key: 'guide_price_display',
			position: 'left',
			// before: '<font color=green >指导价格: ¥</font>'
		},
		{
			key: 'cost_price_display',
			position: 'left',
			// before: '<font color=red >成本价格: ¥</font>'
		},
		{
			key: 'purchase_price_display',
			position: 'left',
			// before: '<font color=green >采购价格: ¥</font>'
		},
		{
			key: 'current_stock_num_display',
			position: 'left',
			// before: '<font color=red >当前库存: ¥</font>'
		}
	];

	$scope.data = [];

	
	

	$scope.$watch('saleobj', function (newValue) {
		// $scope.findinfoList();
	}, true);

	$scope.options = {
		state: 'state',
		checkbox: false
	}

	$scope.changeShow = function(){
		$scope.util.showFlag = !$scope.util.showFlag;
	}

};